<?php
class Admin_IndexController extends Zend_Controller_Action
{
	private $model;
	public function init()
	{
		$this->model = new Admin_Model_User();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/admin";
		$dirlayout = APPLICATION_PATH . "/layouts/admin";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/login.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slidedown.js');
		//$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		//Zend_Layout::startMvc($option);
	}
	public function indexAction()
	{
		//$this->_helper->viewRenderer->setNoRender();
		//$this->_helper->layout->disableLayout();
	}
	public function loginAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		//$this->_helper->layout->disableLayout();
		 if ($this->_request->isPost())
		{
			$db = Zend_Registry::get('connectDB');
			$auth = Zend_Auth::getInstance();
			$authtable = new Zend_Auth_Adapter_DbTable($db);
			$authtable->setTableName('users')->setIdentityColumn('username')->setCredentialColumn('password');
			$user = $this->_request->getParam('username');
			$pass = $this->_request->getParam('pass');
			$authtable->setIdentity($user);
			$authtable->setCredential(md5($pass));
			$result = $auth->authenticate($authtable);
			if ($result->isValid())
			{
			$data = $authtable->getResultRowObject(null,'password');
			$auth->getStorage()->write($data);
			if($data->id_group == 1 && $data->active == 1)
				$this->_redirect('/admin/viewlog');
			$update = $this->model->status($data->id_user,1);
			}
		else
			$this->_redirect('/admin/');
			
		} 
	}
	public function viewlogAction()
	{
		
	}
	public function logoutAction()
	{
		$auth = Zend_Auth::getInstance();
		$update = $this->model->status($auth->getIdentity()->id_user, 0);
		$auth->clearIdentity();
		$this->_redirect('/');
	}
}